<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>营销管理</el-breadcrumb-item>
                <el-breadcrumb-item>客服问答</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="customerFormData" class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="customerFormData.submodule" placeholder="请输入模块名称"></el-input>
                </el-form-item>

                <el-form-item >
                    <el-input v-model="customerFormData.question" placeholder="请输入问题"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="select">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="customerdata"
                        :border="true"
                        stripe
                        style="width: 100%;font-size: 20px">
                    <el-table-column
                            prop="customerid"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="submodule"
                            label="模块名称"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="question"
                            label="问题">
                    </el-table-column>

                    <el-table-column
                            prop="answer"
                            label="答案">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="添加时间">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.customerid)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.customerid)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>

            <div>
                <el-dialog
                        title="添加"
                        :visible.sync="upForm"
                        width="30%">
                <span>
                    <el-form label-position="left" label-width="80px" :model="customerTypeForm"  :rules="rules" ref="FromRef">
                          <el-form-item label="编号" v-if="show">
                        <el-input v-model="customerTypeForm.customerid"></el-input>
                      </el-form-item>
                      <el-form-item label="模块名称" prop="submodule">
                        <el-input v-model="customerTypeForm.submodule"></el-input>
                      </el-form-item>
                      <el-form-item label="问题" prop="question">
                        <el-input v-model="customerTypeForm.question"></el-input>
                      </el-form-item>
                      <el-form-item label="答案"  prop="answer">
                        <el-input v-model="customerTypeForm.answer"></el-input>
                      </el-form-item>
                        <el-form-item label="添加时间"  prop="addtime">
                        <el-input type="date" v-model="customerTypeForm.addtime"></el-input>
                      </el-form-item>

                    </el-form>
                </span>
                    <span slot="footer" class="dialog-footer">
                    <el-button @click="upForm= false">取 消</el-button>
                    <el-button type="primary" @click="confirm">确 定</el-button>
                </span>
                </el-dialog>
            </div>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Customer",
        data(){
            return {
                //条件搜索框对象
                customerFormData:{},

                //隐藏id
                show:false,

                //校验规则
                rules:{
                    submodule:[
                        { required: true, message: '模块名不能为空值', trigger: 'blur' },
                    ],
                    question:[
                        { required: true, message: '请输入问题', trigger: 'blur' },
                    ],
                    answer:[
                        { required: true, message: '请输入答案', trigger: 'blur' },
                    ],
                    addtime:[
                        {required: true, message: '选择添加时间', trigger: 'blur'}
                    ],

                },

                //整体表格数据
                customerdata:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,

                customerTypeForm: {},

                //弹出表单数据
                upForm:false,

            }
        },
        created() {
            //初始化客服页面
            this.initcustomerTable()
        },
        methods:{
            //初始化用户列表
            initcustomerTable(){
                var that=this;
                this.$axios.post(`clms-market/market/customer/initcustomerTable/${this.currentPage}/${this.pageSize}`,this.customerFormData).then(function (resp) {
                    that.customerdata=resp.data.result.records;
                    that.total=resp.data.result.total;
                })
            },

            //添加和修改
            confirm(){
                var that=this;
                this.$refs.FromRef.validate((valid) => {
                    if (valid) {
                        if(this.customerTypeForm.customerid!=null){
                            this.$axios.put("clms-market/market/customer/update",this.customerTypeForm).then(function (result) {
                                that.upForm=false;
                                that.customerTypeForm={};
                                that.initcustomerTable();
                            })
                        }else {
                            this.$axios.post("clms-market/market/customer/insert",this.customerTypeForm).then(function (result) {
                                that.upForm = false;
                                that.initcustomerTable();
                            })
                        }

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },

            //条件查询按钮触发
            select(){
                this.initcustomerTable();
            },

            //添加按钮触发
            insert(){
                this.upForm=true;
                this.customerTypeForm={};
            },


            //根据id查询回显
            selectById(id){
                var that=this;
                this.$axios.get(`clms-market/market/customer/selectById/${id}`).then(function (returnResult) {
                    that.upForm=true;
                    that.customerTypeForm=returnResult.data.result.huixian;
                    console.log(returnResult)
                })
            },

            //删除
            deleteById(id){
                var that=this;
                this.$confirm('确认删除此行?', '提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("clms-market/market/customer?id="+id).then(function (result)  {
                        that.initcustomerTable();

                    })
                })


            },

            //当改变每页显示的条数时触发该方法
            handleSizeChange(v){
                this.pageSize=v;
                this.initcustomerTable();

            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage=v;
                this.initcustomerTable();
            },

            switchStatus(customerid,status){
                //改变状态
            }
        }
    }
</script>
<style scoped>
</style>